﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Collections;

namespace SerialPort
{
    
    public partial class Form1 : Form
    {
        bool stoptb2 = false;
        public static string path;
        public static string info;
        public static web webForm;
        public static Jisuan jisuanForm;
        bool zdfs=false;
        bool tzfs = false;
        bool zhouqi = false;
        private int txd=0;
        private int rxd=0;
        //十六进制显示标志
        bool ishex = false;
        string hex1;

        //委托
        delegate void HandleInterfaceUpdateDelegate(string text);  //委托，此为重点
        HandleInterfaceUpdateDelegate interfaceUpdateHandle = null;
        public Form1()
        {
            InitializeComponent();
            webForm=new web();
            jisuanForm = new Jisuan();
            
        }
     
        private void Form1_Load(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = "DeepCyan.ssk";//获取皮肤
            txd = 0;
            rxd = 0;
            
            comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());//从系统获取已有串口
            //十进制转二进制
            //textBox1.Text=Convert.ToString(15, 2);
            //十进制转八进制
            //textBox1.Text=Convert.ToString(15, 8);
            //十进制转十六进制
            //textBox1.Text=Convert.ToString(15, 16).ToUpper();
            //二进制转十进制
            //textBox1.Text=Convert.ToString(Convert.ToInt32("100111101″,2));
            //八进制转十进制
            //textBox1.Text=Convert.ToString(Convert.ToInt32("76″, 8));
            //十六进制转十进制
            //textBox1.Text=Convert.ToString(Convert.ToInt32("FF", 16));
            if (File.Exists(Application.StartupPath + @"\SerConfig.wjs"))
            {
                using (StreamReader sr = new StreamReader(Application.StartupPath + @"\SerConfig.wjs", Encoding.UTF8))
                {
                    comboBox1.SelectedItem = sr.ReadLine();
                    comboBox2.SelectedItem = sr.ReadLine();
                    comboBox3.SelectedItem = sr.ReadLine();
                    comboBox4.SelectedItem = sr.ReadLine();
                    comboBox5.SelectedItem = sr.ReadLine();
                }
            }
            else
            {
                comboBox1.SelectedIndex = 0;
                comboBox2.SelectedIndex = 6;
                comboBox3.SelectedIndex = 0;
                comboBox4.SelectedIndex = 0;
                comboBox5.SelectedIndex = 0;
            }
            Form.CheckForIllegalCrossThreadCalls = false;
            if (File.Exists(Application.StartupPath + @"\jilu.txt"))
            {
                button12.Enabled = true;
            }
            if (comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedIndex = 0;

                //串口变量初始化
            
                serialPort1.RtsEnable = true;
                serialPort1.ReceivedBytesThreshold = 1;//设置 DataReceived 事件发生前内部输入缓冲区中的字节数
                serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);    //DataReceived事件委托

            }
            else
            {
               textBox3.Text += string.Format("{0}：",DateTime.Now.ToLongTimeString())+"未检测到串口\r\n";
               
            }
            //委托实例化
            interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(UpdateTextBox);  //实例化委托对象 
        }

        private void UpdateTextBox(string text)
        {
            if (stoptb2 == false)
            {
                textBox2.Text += text;
            }

            //更新接收显示
            receive.Text = String.Format(" {0}", rxd);
        }
        public ArrayList commmand = new ArrayList();
        private void button4_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.InitialDirectory = @"E:";
            this.openFileDialog1.Filter = "hex 文件(*.hex)|*.hex|txt 文件(*.txt)|*.txt";
            this.openFileDialog1.FilterIndex = 1;
            this.openFileDialog1.RestoreDirectory = true;
            this.openFileDialog1.Title = "选择发送文件";
            if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                textBox3.Text +=string.Format("{0}：",DateTime.Now.ToLongTimeString())+"文件已添加！"+"\r\n"+"文件路径："+"\r\n";
                textBox3.Text += this.openFileDialog1.FileName+"\r\n";
            }
            path = this.openFileDialog1.FileName;//获取文件的发送路径
            
        }

        
        private void button1_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                if(tzfs)
                {
                    timer3.Enabled = false;
                    timer1.Enabled = false;
                    progressBar2.Value =progressBar2.Minimum;
                    textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "已停止自动发送或自动发送已完成！" + "\r\n";
                    button1.Text = "自动发送";
                    tzfs = false;
                    checkBox2.Enabled = true;
                    groupBox10.Enabled = true;
                    progressBar1.Value = progressBar1.Minimum;
                    zhouqi = false;
                }
                else
                {
                    if (zdfs)
                    {
                        if (textBox1.Text == string.Empty)
                        {
                            MessageBox.Show("发送内容为空！", "提示");
                            textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "发送内容为空！" + "\r\n";
                            return;
                        }
                        else
                        {
                            if (textBox4.Text == null || textBox4.Text == "0" || textBox4.Text == "")
                            {
                                
                            }
                            else
                            {
                                timer1.Interval = Convert.ToInt32(textBox4.Text);
                            }

                                timer3.Enabled = true;
                            
                            
                            textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "正在自动发送。。。！" + "\r\n";
                            button1.Text = "停止发送";
                            tzfs = true;
                            checkBox2.Enabled = false;
                            groupBox10.Enabled = false;
                        }
                    }
                    else
                    {
                        if (radioButton1.Checked == true)
                        {
                            //byte[] data = Encoding.Unicode.GetBytes(textBox1.Text);
                            //string str = Convert.ToBase64String(data);
                            if (checkBox1.Checked == true)
                            {
                                if (textBox1.Text == string.Empty)
                                {
                                    MessageBox.Show("请输入要发送的十六进制数据！", "提示");
                                    textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "请输入要发送的十六进制数据！" + "\r\n";
                                    return;
                                }
                                else
                                {
                                    
                                    string sd = textBox1.Text;
                                    string zh=sd.Substring(0,sd.Length);
                                    int zh1 = Convert.ToInt32(zh);
                                    string hex = zh1.ToString("x").ToUpper();
                                    hex1 = hex;
                                    
                                    try//防止出错
                                    {
                                       
                                        serialPort1.Write(hex);
                                        txd = txd + sd.Length;
                                        send.Text = txd.ToString();
                                         textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "十六进制数据发送成功！" + "\r\n";
                                    }
                                    catch(Exception err)
                                    {//如果出错就执行此块代码
                                        if (serialPort1.IsOpen)
                                            serialPort1.Close();//如果是写数据时出错，此时窗口状态为开，就应关闭串口，防止下次不能使用，串口是不能重复打开和关闭的
                                        MessageBox.Show(err.ToString(), "错误");
                                        textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "错误：\r\n" + err.ToString() + "\r\n";
                                    }
                                 
                                    
                                }
                            }
                            else
                            {
                                if (textBox1.Text == string.Empty)
                                {
                                    MessageBox.Show("发送内容为空！", "提示");
                                    textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "发送内容为空！" + "\r\n";
                                    return;
                                }
                                else
                                {
                                    try
                                    {
                                        serialPort1.WriteLine(textBox1.Text);
                                    }
                                    catch(Exception err)
                                    {
                                        if (serialPort1.IsOpen)
                                            serialPort1.Close();//如果是写数据时出错，此时窗口状态为开，就应关闭串口，防止下次不能使用，串口是不能重复打开和关闭的
                                        MessageBox.Show(err.ToString(), "错误");
                                        textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "错误：\r\n" + err.ToString() + "\r\n";
                                    }
                                    string sd1 = textBox1.Text;
                                    txd = txd + sd1.Length;
                                    send.Text = txd.ToString();
                                    textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "字符串数据发送成功！" + "\r\n";
                                }
                            }
                        }
                        if (radioButton2.Checked == true)
                        {
                            if (path == string.Empty)
                            {
                                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "未选择发送的文件或者未将发送的文件拖至发送框！" + "\r\n";
                            }
                            else
                            {
                                Byte[] BSendTemp = new Byte[8192]; //建立临时字节数组对象
                                BSendTemp = File.ReadAllBytes(path);//使用File.ReadAllBytes("文件路径")复制文件到BYTE型数组
                                serialPort1.Write(BSendTemp, 0, 8192);//发送数据

                                txd = txd + BSendTemp.Length;
                                send.Text = txd.ToString();
                                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "文件发送成功！" + "\r\n";
                                path = string.Empty;
                            }
                        }

                    }
                }
            }
            else
            {
                MessageBox.Show("串口未打开！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "串口未打开！" + "\r\n";
            }
        }



        

        private void button3_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                MessageBox.Show("串口已经打开", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "串口已经打开！" + "\r\n";
                return;
            }
            //串口
           
            if(comboBox1.Text==string.Empty)
            {
                MessageBox.Show("请选择串口", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "请选择串口！" + "\r\n";
                return;
            }
            //波特率
            
            if(comboBox2.Text==string.Empty)
            {
                MessageBox.Show("请选择波特率","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "请选择波特率！" + "\r\n";
                return;
            }
            if (comboBox4.Text == string.Empty)
            {
                MessageBox.Show("请选择数据位", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "请选择数据位！" + "\r\n";
                return;
            }
            //校验位
            switch (comboBox5.SelectedIndex)
            {
                case 0:
                    serialPort1.Parity = Parity.None;
                    break;
                case 1:
                    serialPort1.Parity = Parity.Odd;
                    break;
                case 2:
                    serialPort1.Parity = Parity.Even;
                    break;
                case 3:
                    serialPort1.Parity = Parity.Mark;
                    break;
                case 4:
                    serialPort1.Parity = Parity.Space;
                    break;
            }
            //数据位

            serialPort1.DataBits = Convert.ToInt32(comboBox4.SelectedItem.ToString());
            //停止位
            switch (comboBox3.SelectedIndex)
            {
                case 0:
                    serialPort1.StopBits = StopBits.One;
                    break;
                case 1:
                    serialPort1.StopBits = StopBits.Two;
                    break;
                case 2:
                    serialPort1.StopBits = StopBits.OnePointFive;
                    break;
            }
           

            serialPort1.BaudRate = int.Parse(comboBox2.Text);
            try
            {
                serialPort1.PortName = comboBox1.SelectedItem.ToString();
                serialPort1.Open();
                MessageBox.Show("串口" + comboBox1.SelectedItem.ToString() + "打开成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "串口" + comboBox1.SelectedItem.ToString() + "打开成功！" + "\r\n";
                button7.Enabled = true;
                button10.Enabled = true;
                button6.Enabled = false;
                comboBox1.Enabled = false;
                comboBox2.Enabled = false;
                comboBox3.Enabled = false;
                comboBox4.Enabled = false;
                comboBox5.Enabled = false;
                groupBox1.Enabled = true;
                listBox1.Enabled = true;
                label9.Text = "打开";
                button3.Enabled = false;
                button2.Enabled = false;
            }
            catch
            {
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "串口"+comboBox1.SelectedItem.ToString()+"打开失败！" + "\r\n"+"请重新选择串口或检查串口是否正确连接！"+ "\r\n";
                try
                {
                    comboBox1.SelectedIndex = comboBox1.SelectedIndex + 1;
                }
                catch
                {
                    comboBox1.SelectedIndex = 0;
                }
                serialPort1.Close();
            }
            
            
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            button7.Enabled = false;
            button6.Enabled = true;
            comboBox1.Enabled = true;
            comboBox2.Enabled = true;
            comboBox3.Enabled = true;
            comboBox4.Enabled = true;
            comboBox5.Enabled = true;
            groupBox1.Enabled = false;
            listBox1.Enabled = false;
            button2.Enabled = true;
            listBox1.BackColor = Color.Gainsboro;
            MessageBox.Show("串口"+comboBox1.SelectedItem.ToString()+"关闭！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            button3.Enabled = true;
            textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "串口" + comboBox1.SelectedItem.ToString() + "关闭！" + "\r\n";
            label9.Text = "关闭";
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button8_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            
        }

        private void button9_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            serialPort1.Close();
            this.Close();
        }

        private void radioButton2_Click(object sender, EventArgs e)
        {
           
            
                 textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "请选择发送的文件或将发送的文件拖至发送框！" + "\r\n";
                button4.Enabled = true;
                listBox1.AllowDrop = true;
                listBox1.BackColor = Color.White;
                listBox1.Enabled = true;
            
           
        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "请输入发送的字符！" + "\r\n";
            button4.Enabled = false;
            listBox1.AllowDrop = false;
            listBox1.BackColor = Color.Gainsboro;
            listBox1.Enabled = false;
        }
        //保存配置
        private void button10_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                using (StreamWriter sw = new StreamWriter(Application.StartupPath + @"\SerConfig.wjs", false, Encoding.UTF8))
                {
                    sw.WriteLine(comboBox1.SelectedItem.ToString());
                    sw.WriteLine(comboBox2.SelectedItem.ToString());
                    sw.WriteLine(comboBox3.SelectedItem.ToString());
                    sw.WriteLine(comboBox4.SelectedItem.ToString());
                    sw.WriteLine(comboBox5.SelectedItem.ToString());
                    sw.Flush();
                }
                MessageBox.Show("配置已成功保存 下次程序启动后会自动读取配置", "提示");
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "配置已成功保存 下次程序启动后会自动读取配置！" + "\r\n";
            }
            else
            {
                MessageBox.Show("只有成功打开了串口 才能保存配置", "提示");
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "只有成功打开了串口 才能保存配置！" + "\r\n";
            }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            textBox3.Clear();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {

        }



        private void button13_Click(object sender, EventArgs e)
        {

            
                using (StreamWriter jl = new StreamWriter(Application.StartupPath + @"\jilu.txt", false, Encoding.UTF8))
                {
                    
                    jl.Write(textBox3.Text + "\r\n");
                    jl.Flush();
                }
                MessageBox.Show("记录已成功保存", "提示");
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "记录已成功保存！" + "\r\n";
                button12.Enabled = true;
            
        }

        private void button12_Click(object sender, EventArgs e)
        {
            if (File.Exists(Application.StartupPath + @"\jilu.txt"))
            {
                using (StreamReader ck = new StreamReader(Application.StartupPath + @"\jilu.txt", Encoding.UTF8))
                {
                    textBox2.Text += ck.ReadToEnd()+"\r\n";
                }
            }
            else
            {
                MessageBox.Show("无保存记录！", "提示");
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "无保存记录！" + "\r\n";
            }
            Form.CheckForIllegalCrossThreadCalls = false;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                MessageBox.Show("串口未关闭，不能恢复默认设置","提示");
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "串口未关闭，不能恢复默认设置！" + "\r\n";
                return;
            }
            else
            {
                comboBox1.SelectedIndex = 0;
                comboBox2.SelectedIndex = 6;
                comboBox3.SelectedIndex = 0;
                comboBox4.SelectedIndex = 0;
                comboBox5.SelectedIndex = 0;
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "已恢复默认设置！" + "\r\n";
            }
        }
         private void Form1_DragEnter(object sender, DragEventArgs e)
        {

        }
        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            
        }

        private void textBox3_TextChanged_1(object sender, EventArgs e)
        {
            textBox3.Select(textBox3.Text.Length, 0);
            textBox3.ScrollToCaret();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            textBox2.Select(textBox2.Text.Length, 0);
            textBox2.ScrollToCaret();
        }

        private void button14_Click(object sender, EventArgs e)
        {
       
        }

        private void button14_Click_1(object sender, EventArgs e)
        {
            Form1.webForm = new web();
            Form1.webForm.Show();
        }

        private void listBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.All;
            else e.Effect = DragDropEffects.None;
        }

        private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            string[] filePath = (string[])e.Data.GetData(DataFormats.FileDrop);
            textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString())  + "文件已添加！\r\n"+ "文件路径：" + "\r\n";
            for (int i = 0; i < filePath.Length; i++)
            {
                path = filePath[i];//获取文件路径
               
            }

            textBox3.Text += path + "\r\n";
            
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox2.Checked==true)
            {
                groupBox10.Enabled =true;
                button1.Text = "自动发送";
                zdfs = true;
            }
            else
            {
                groupBox10.Enabled = false;
                button1.Text = "发送";
                zdfs = false;
            }
        }

        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            progressBar1.PerformStep(); 
            if(progressBar1.Value==progressBar1.Maximum)
            {
               progressBar1.Value = progressBar1.Minimum;
            }
            if (checkBox1.Checked)
            {
                serialPort1.Write(hex1);

            }
            else
            {
                serialPort1.Write(textBox1.Text);
            }
            string sd2 = textBox1.Text;
            txd = txd + sd2.Length;
            send.Text = txd.ToString();
            if (textBox4.Text == null || textBox4.Text == ""|| textBox4.Text == "0")
            {
                zhouqi = true;
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "自动发送已完成！" + "\r\n";
                progressBar1.Value = progressBar1.Minimum;

            }
        }



        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void button16_Click(object sender, EventArgs e)
        {
            Form1.jisuanForm=new Jisuan();
            Form1.jisuanForm.Show();
            return;
        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabelNowTime.Text = string.Format("{0}", DateTime.Now.ToLongTimeString());
        }

        private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
        {
            txd = 0;
            rxd = 0;
            send.Text = "0";
            receive.Text = "0";
            
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            progressBar2.PerformStep();
            if (textBox5.Text == string.Empty||textBox5.Text=="无")
            {

            }
            else
            {
                
                int a=int.Parse(textBox5.Text);
                if (0 <= a)
                {
                    progressBar2.Maximum = a*10;
                }
                else
                {
                    textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "输入的时间有误，请重新输入！\r\n" ;
                    return;
                }
            }
            if(progressBar2.Value==progressBar2.Maximum)
            {
                timer1.Enabled = true;
            }
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            if(zhouqi)
            {
                timer1.Enabled = false;
            }
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int readlen =serialPort1.BytesToRead;

            //接收计数
            rxd += readlen;
            receive.Text = rxd.ToString();

            byte[] readBuffer = new byte[serialPort1.ReadBufferSize];
            serialPort1.Read(readBuffer, 0, readBuffer.Length);
            if (ishex)
            {
                string readbuf = null;

                //十六进制格式化数据
                for (int i = 0; i < readlen; i++)
                {
                    string s = String.Format("{0:X}", Convert.ToInt32(readBuffer[i]));
                    if (s.Length > 1)
                    {
                        readbuf += s + " ";
                    }
                    else
                    {
                        readbuf += "0" + s + " ";
                    }
                }

                this.Invoke(interfaceUpdateHandle, readbuf);

            }
            else
            {


                this.Invoke(interfaceUpdateHandle, new string[] { Encoding.UTF8.GetString(readBuffer) });

            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                ishex = true;
                
            }
            else
            {
                ishex = false;
            }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            if (stoptb2 == false)       //停止显示标志置位
            {
                stoptb2 = true;
                button15.Text = "继续显示";
            }
            else
            {
                stoptb2 = false;                    //停止显示标志复位
                button15.Text = "停止显示";
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "注意：\r\n" + "只能进行将数字十六进制发送，且数字范围 -2147483648-2147483648：\r\n";
            }
        }

        private void Form1_Leave(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
            textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "串口扫描成功，请选择串口！\r\n";
            if (comboBox1.Items.Count > 0)
            {
                comboBox1.SelectedIndex = 0;
            }
            else
            {
                textBox3.Text += string.Format("{0}：", DateTime.Now.ToLongTimeString()) + "未检测到串口\r\n";
            }
        }

        
  
    }
}
